package com.aaa.service.fpa.impl;

import com.aaa.dao.fpa.TFpaSubMapper;
import com.aaa.entity.MyPage;
import com.aaa.entity.fpa.TFpaSub;
import com.aaa.service.fpa.TFpaSubService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * (TFpaSub)表服务实现类
 *
 * @author 李江波
 * @since 2022-10-22 17:05:01
 */
@Service
public class TFpaSubServiceImpl implements TFpaSubService {
   @Autowired
   private TFpaSubMapper tFpaSubMapper;

   @Override
   public void dealFile(MultipartFile file) {

      // 如果试题已经存在就不让再导入了
      int i = tFpaSubMapper.selectCount(null);
     if(i>0){
        return;
     }else {
        // 将excel中的数据导入到数据库中
//      InputStream is = null;
//         String fileUrl="https://yuyonglitest.oss-cn-beijing.aliyuncs.com/%E9%A2%98%E7%9B%AE%E6%B8%85%E5%8D%952.xlsx";
//      try {
//         is = new URL(fileUrl).openStream();
//      } catch (IOException e) {
//         e.printStackTrace();
//      }
        InputStream is = null;
        try {
           is = file.getInputStream();
        } catch (IOException e) {
           e.printStackTrace();
        }
        //1.进行读取数据，TFpaSub是我的pojo类，
        //2.new FileListener(fileService)这个是监听器，主要用来读取数据的
        //3.特别注意的是fileService这个service，我上面有注入进去 @Autowired/@Resource，切记不要new会报错
        //EasyExcel.read(is, TFpaSub.class, new FileListener(fileService)).sheet("FPA性格测试").doRead();
        EasyExcel.read(is, TFpaSub.class, new AnalysisEventListener<TFpaSub>() {
           /**
            * 单次缓存的数据量
            */
           public static final int BATCH_COUNT = 100;
           /**
            * 临时存储
            */
           private List<TFpaSub> cachedDataList = new ArrayList<>();

           @Override
           public void invoke(TFpaSub data, AnalysisContext context) {
              cachedDataList.add(data);
              if (cachedDataList.size() >= BATCH_COUNT) {
                 saveData();
                 // 存储完成清理 list
                 cachedDataList = new ArrayList<>();
              }
           }

           @Override
           public void doAfterAllAnalysed(AnalysisContext context) {
              saveData();
           }

           /**
            * 加上存储数据库
            */
           private void saveData() {
//            log.info("{}条数据，开始存储数据库！", cachedDataList.size());
//            log.info("存储数据库成功！");
              save(cachedDataList);
           }
        }).sheet("FPA性格测试").doRead();
     }
   }

   @Override
   public void save(List<TFpaSub> list) {
      tFpaSubMapper.insertFtaSubjects(list);
   }

    @Override
    public PageInfo getAll(MyPage myPage) {

       PageHelper.startPage(myPage.getPage(),myPage.getLimit());

        return new PageInfo(tFpaSubMapper.selectAll());
    }
}

